package com.fenghongzhang.day007;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    private LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);


        ll = (LinearLayout) findViewById(R.id.ll);





        List<Fragment> list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());

        //建立集合,收集图片
        final List<ImageView> imgList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(this);
            if (i == 0){
                imageView.setImageResource(R.drawable.cricle_t);
            }else{
                imageView.setImageResource(R.drawable.cricle_f);
            }
            imgList.add(imageView);
            ll.addView(imageView);

//            ll.set
//            ll.generateLayoutParams()
        }


        FGAdapter fgAdapter = new FGAdapter(getSupportFragmentManager(), list);
        vp.setAdapter(fgAdapter);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                /**
                 * 1,如果是第几页,就是修为红色
                 * 2,其他的在改成灰色.
                 */
                for (int i = 0; i < imgList.size(); i++) {
                    ImageView imageView = imgList.get(i);
                    if(i == position){
                        imageView.setImageResource(R.drawable.cricle_t);
                    }else{
                        imageView.setImageResource(R.drawable.cricle_f);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
